package Homeworks.Ityxb_7_1;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class RecordBooksOrder {
    public static ArrayList<Books>booksList=new ArrayList<Books>();

    public static void main(String[] args) {
        //初始化书架
        init();
        //将暑假上的所有图书信息打印出来
        for (Books book:booksList){
            System.out.println(book);
        }

        while (true){
            Scanner sc=new Scanner(System.in);

            System.out.println("请输入图书编号");
            int bookId=sc.nextInt();
            //根据输入的图书编号获取图书信息
            Books stockBooks=getBooksById(bookId);
            if(stockBooks!=null){
                System.out.println("当前图书信息："+stockBooks);
                System.out.println("请输入购买数量：");
                int bookNumber=sc.nextInt();
                //购买判断
                if(bookNumber<stockBooks.getNumber()){
                    System.out.println("库存不足，购买失败");
                }else {
                    //购买：将购买的图书的信息封装成books对象
                    Books books=new Books(stockBooks.getId(),stockBooks.getName(),stockBooks.getPrice(),stockBooks.getNumber(),stockBooks.getMoney(),stockBooks.getPublish());

                    //将本条购买信息保存至本地文件
                    //拼接文件名称
                    Date date=new Date();
                    DateFormat format=new SimpleDateFormat("yyyyMMdd");

                    String fileName="销售记录"+format.format(date)+".csv";


                    try {
                        FileInputStream in=new FileInputStream(fileName);
                        if(in!=null){
                            //文件存在
                            in.close();
                            //打开文件，追加数据
                            saveBooks(fileName,true,books);
                        }
                    }catch (FileNotFoundException e){
                        //文件不存在时会产生的异常
                        saveBooks(fileName,false,books);
                    }catch (IOException e){
                        e.printStackTrace();
                    }



                    //修改库存数量
                    stockBooks.setNumber(stockBooks.getNumber()-bookNumber);

                }
            }else {
                System.out.println("没有找到该图书");
            }

        }

    }
    public static void init(){
        Books good1= new Books(101,"书本101",44.5,100,4450,"出版社1");
        Books good2= new Books(102,"书本102",54,100,5400,"出版社2");
        Books good3= new Books(103,"书本103",99,100,9900,"出版社3");

        booksList.add(good1);
        booksList.add(good2);
        booksList.add(good3);
    }
    public static Books getBooksById(int bookId){
        for (Books thisBooks:booksList){
            if(thisBooks.getId()==bookId)
            return thisBooks;
        }
        return null;
    }
    /**
     * 通过label判断是修改还是新建
     * fileName 文件名
     * label：true修改，false新建
     * books 图书信息
     * */
    public  static void saveBooks(String fileName,boolean label,Books books){
        try {

            BufferedOutputStream out=null;
            StringBuffer sbf=new StringBuffer();

            if (label){
                //修改文件
                out=new BufferedOutputStream(new FileOutputStream(fileName,true));
            }else {
                //新建文件
                out=new BufferedOutputStream(new FileOutputStream(fileName));
                //输入表头信息
                String[] fieldSort={"图形编号","图书名称","购买数量","单价","总价","出版社"};
                for (String field:fieldSort){
                    sbf.append(field).append(",");

                }
            }
            sbf.append("\r\n");
                sbf.append(books.getId()).append(",");
                sbf.append(books.getName()).append(",");
                sbf.append(books.getNumber()).append(",");
                sbf.append(books.getPrice()).append(",");
                sbf.append(books.getMoney()).append(",");
                sbf.append(books.getPublish()).append(",");
            //通过操作文件的输出流，将图书信息写入到文件中
            String result =sbf.toString();
            byte[] bys=result.getBytes();
            out.write(  bys   );
            out.close();
        }catch (IOException e){
            e.printStackTrace();
        }


    }
}
